package se.webbzon.oschi01.json;

import java.io.IOException;
import java.io.PrintStream;

public class JsonNumber implements JsonLeaf<Double> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4039602702736940324L;
	
	protected double d;
	
	public JsonNumber() {
		this(0.0);
	}
	
	public JsonNumber(JsonNumber other) {
		this(other.d);
	}
	
	public JsonNumber(double d) {
		this.d = d;
	}

	@Override public Type getType() {
		return JsonType.Type.Number;
	}
	
	@Override public String toString() {
		return Double.toString(d);
	}
	
	@Override public int hashCode() {
		return ((Double) d).hashCode();
	}

	@Override public Double getValue() {
		return d;
	}

	@Override public void setValue(Double d) {
		this.d = d == null ? 0 : d;
	}
	
	@Override public JsonNumber clone() {
		return new JsonNumber(this);
	}
	
	@Override public int compareTo(JsonLeaf<Double> arg0) {
		return getValue().compareTo(arg0.getValue());
	}

	@Override public boolean equals(Object obj) {
		if (obj instanceof JsonLeaf)
			return getValue().equals(((JsonLeaf<?>) obj).getValue());
		else
			return false;
	}
	
	@Override public void printTo(PrintStream ps) throws IOException {
		ps.print(toString());
	}
	
}
